import asyncio
import itertools
import sys



async def spin(msg):
    write, flush = sys.stdout.write, sys.stdout.flush
    status = ''
    for char in itertools.cycle('|/-\\'):
        status = char + ' ' + msg
        write(status)
        flush()
        write('\x08' * len(status))
        try:
            await asyncio.sleep(.1)
        except asyncio.CancelledError:
            break
    write(' ' * len(status) + '\x08' * len(status))


async def slow_function():
    await asyncio.sleep(3)
    return 42


async def supervisor():
    # way 1
    spinner = asyncio.create_task(spin('thinking'))
    print('spinner object:', spinner)
    result = await slow_function()
    spinner.cancel()
    return result

if __name__ == "__main__":
    # loop = asyncio.get_event_loop()
    # result = loop.run_until_complete(supervisor())
    # loop.close()
    # This function always creates a new event loop and closes it at the end.
    # It should be used as a main entry point for asyncio programs,
    # and should ideally only be called once.
    result = asyncio.run(supervisor())
    print('Answer: ', result)